<template>
  <div class="container">
    <div class="app-container">
      <el-tree :expand-on-click-node="false" :data="depts" :props="defaultProps" default-expand-all @node-click="handleNodeClick">

        <!-- 节点结构 -->
        <template v-slot="{data}">
          <el-row style="width: 100%;height;40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }} </el-col>
            <el-col :span="4">
              <!--   管理员名字 -->
              <span class="tree-manager">{{ data.managerName }}</span>
              <!--   下拉菜单 -->
              <el-dropdown trigger="click" @command="handleCommand($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>
            </el-col>

          </el-row>
        <!--  -->
        </template>
      </el-tree>
      <AddDept ref="addDept" :show-dialog.sync="showDialog" :current-node-id="currentNodeId" @updateDepartment="getDepartment" />
    </div>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { AddDept },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentNodeId: null
    }
  },
  // updateDepartment() {
  //   this.getDepartment()
  // },
  created() {
    this.getDepartment()
  },
  methods: {
    handleNodeClick(data) {
      // console.log('data', data)
    },
    async getDepartment() {
      const result = await getDepartment()
      // this.depts = result
      // console.log(transListToTreeData(result, 0))
      this.depts = transListToTreeData(result, 0)
    },
    handleCommand(command, id) {
      // console.log(command, id)
      //  新建
      if (command === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      } else if (command === 'edit') {
        //  编辑
        this.showDialog = true
        this.currentNodeId = id // 记录id
        //  利用 thia.$nextTick  处理 DOM 异步 更新
        // console.log(this.$refs.addDept)
        this.$refs.addDept.getDepartmentDetail()
      } else {
        //  删除部门
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartment(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getDepartment()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
}
</script>

<style scoped>
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  .demonstration {
    display: block;
    color: #8492a6;
    font-size: 14px;
    margin-bottom: 20px;
  }.el-dropdown{
    margin-left: 20px;
  }
</style>
